﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace MinhTuan
{
    public class Members
    {
            #region //attribute
            private Int32 _Members_Id;
            private String _Members_Name;
            private String _Members_Login;
            private String _Members_Password;
            private String _Members_Address;
            private String _Members_Tel;
            private DateTime _Members_DateJoin;
            private String _Members_Mail;
            private DateTime _Members_LastLogin;
            private String _Members_City;
            private Boolean _IsActive;
            #endregion
            #region //contrucstor
            public Members()
            {

            }
            public Members(
                Int32 Members_Id,
                String Members_Name,
                String Members_Login,
                String Members_Password,
                String Members_Address,
                String Members_Tel,
                DateTime Members_DateJoin,
                String Members_Mail,
                DateTime Members_LastLogin,
                String Members_City,
                Boolean IsActive
                )
            {
                _Members_Id = Members_Id;
                _Members_Name = Members_Name;
                _Members_Login = Members_Login;
                _Members_Password = Members_Password;
                _Members_Address = Members_Address;
                _Members_Tel = Members_Tel;
                _Members_DateJoin = Members_DateJoin;
                _Members_Mail = Members_Mail;
                _Members_LastLogin = Members_LastLogin;
                _Members_City = Members_City;
                _IsActive = IsActive;
            }

            public Int32 Members_Id
            {
                get { return _Members_Id; }
                set { _Members_Id = value; }
            }

            public String Members_Name
            {
                get { return _Members_Name; }
                set { _Members_Name = value; }
            }

            public String Members_Login
            {
                get { return _Members_Login; }
                set { _Members_Login = value; }
            }

            public String Members_Password
            {
                get { return _Members_Password; }
                set { _Members_Password = value; }
            }

            public String Members_Address
            {
                get { return _Members_Address; }
                set { _Members_Address = value; }
            }

            public String Members_Tel
            {
                get { return _Members_Tel; }
                set { _Members_Tel = value; }
            }

            public DateTime Members_DateJoin
            {
                get { return _Members_DateJoin; }
                set { _Members_DateJoin = value; }
            }

            public String Members_Mail
            {
                get { return _Members_Mail; }
                set { _Members_Mail = value; }
            }

            public DateTime Members_LastLogin
            {
                get { return _Members_LastLogin; }
                set { _Members_LastLogin = value; }
            }

            public String Members_City
            {
                get { return _Members_City; }
                set { _Members_City = value; }
            }

            public Boolean IsActive
            {
                get { return _IsActive; }
                set { _IsActive = value; }
            }
            #endregion
            #region//modthod

            public static Int16 AddMember(Members member)
            {
                //try {
                    Byte[] Password=Access.SecurityTool(member.Members_Password);
                    Access.InitAccess();
                    Access.mySqlCommand.CommandText = "Sp_Members_Add";
                    Access.mySqlCommand.Parameters.Add("@Members_Name", SqlDbType.NVarChar).Value = member.Members_Name;
                    Access.mySqlCommand.Parameters.Add("@Members_Login", SqlDbType.NVarChar).Value = member.Members_Login;
                    Access.mySqlCommand.Parameters.Add("@Members_Address", SqlDbType.NVarChar).Value = member.Members_Address;
                    Access.mySqlCommand.Parameters.Add("@Members_City", SqlDbType.NVarChar).Value = member.Members_City;
                    Access.mySqlCommand.Parameters.Add("@Members_Tel", SqlDbType.NVarChar).Value = member.Members_Tel;
                    Access.mySqlCommand.Parameters.Add("@Members_Mail", SqlDbType.NVarChar).Value = member.Members_Mail;
                    Access.mySqlCommand.Parameters.Add("@Members_DateJoin", SqlDbType.DateTime).Value = member.Members_DateJoin;
                    Access.mySqlCommand.Parameters.Add("@Members_LastLogin", SqlDbType.DateTime).Value = member.Members_LastLogin;
                    Access.mySqlCommand.Parameters.Add("@Members_Password", SqlDbType.Binary).Value = Password;
                    Access.mySqlCommand.Parameters.Add("@IsActive", SqlDbType.Bit).Value = 1;
                    Int16 result = 0;
                    result = Convert.ToInt16(Access.mySqlCommand.ExecuteScalar());
                    Access.ClosedAccess();
                    return result;
                //}
                //catch {
                //    Access.ClosedAccess();
                //    return 0;
                
                //}
            
            }
            public static Members Member_login(String Username, String Password)
            {
                Members member = new Members();
                //try
                //{
                    Byte[] pass = Access.SecurityTool(Password);
                    Access.InitAccess();
                    Access.mySqlCommand.CommandText = "Sp_Members_MemberLogin";
                    Access.mySqlCommand.Parameters.Add("@Members_Login", SqlDbType.NVarChar).Value = Username;
                    Access.mySqlCommand.Parameters.Add("@Members_Password", SqlDbType.Binary).Value = pass;
                    SqlDataReader reader = Access.mySqlCommand.ExecuteReader();
                    while (reader.Read())
                    {
                        member.Members_Id = Convert.ToInt16(reader["Members_Id"].ToString());
                        member.Members_Name = reader["Members_Name"].ToString();
                        member.Members_Mail = reader["Members_Mail"].ToString();
                        member.Members_LastLogin = Convert.ToDateTime(reader["Members_LastLogin"]);
                        member.Members_DateJoin = Convert.ToDateTime(reader["Members_DateJoin"]);
                        member.Members_City = reader["Members_City"].ToString();
                        member.Members_Address = reader["Members_Address"].ToString();
                        member.IsActive = Convert.ToBoolean(reader["IsActive"]);
                        member.Members_Tel = reader["Members_Tel"].ToString();

                    }
                    Access.ClosedAccess();
                    reader.Close();
                    return member;
                //}
                //catch {
                //    return member;
                //    Access.ClosedAccess();
                
                //}
            }
            public static Members GetMemberinfo(int Members_Id)
            {
                Members member=new Members();
                //try {
                    Access.InitAccess();
                    Access.mySqlCommand.CommandText = "Sp_Members_GetMemberInfo";
                    Access.mySqlCommand.Parameters.Add("@Members_Id", SqlDbType.Int).Value = Members_Id;
                    SqlDataReader reader = Access.mySqlCommand.ExecuteReader();
                    while (reader.Read())
                    {
                        member.Members_Id = Convert.ToInt16(reader["Members_Id"].ToString());
                        member.Members_Name = reader["Members_Name"].ToString();
                        member.Members_Mail = reader["Members_Mail"].ToString();
                        member.Members_Login = reader["Members_Login"].ToString();
                        member.Members_LastLogin = Convert.ToDateTime(reader["Members_LastLogin"]);
                        member.Members_DateJoin = Convert.ToDateTime(reader["Members_DateJoin"]);
                        member.Members_City = reader["Members_City"].ToString();
                        member.Members_Address = reader["Members_Address"].ToString();
                        member.IsActive = Convert.ToBoolean(reader["IsActive"]);
                        member.Members_Tel = reader["Members_Tel"].ToString();

                    }
                    reader.Close();
                    Access.ClosedAccess();
                    return member;

                //}
                //catch {
                //    Access.ClosedAccess();
                //    return member;
                //}
            }

            public static Boolean UpdateMember(Int32 UserId,String  Name, String Address, String Tel, String Email, String City)
            {
                //try
                //{
                    Access.InitAccess();
                    Access.mySqlCommand.CommandText = "Sp_Members_UpdateMember";
                    Access.mySqlCommand.Parameters.Add("@Member_Id", SqlDbType.Int).Value = UserId;
                    Access.mySqlCommand.Parameters.Add("@Member_Name", SqlDbType.NVarChar).Value = Name;
                    Access.mySqlCommand.Parameters.Add("@Member_Address", SqlDbType.NVarChar).Value = Address;
                    Access.mySqlCommand.Parameters.Add("@Member_Tel", SqlDbType.NVarChar).Value = Tel;
                    Access.mySqlCommand.Parameters.Add("@Member_Email", SqlDbType.NVarChar).Value = Email;
                    Access.mySqlCommand.Parameters.Add("@Member_City", SqlDbType.NVarChar).Value = City;
              
                    Int16 Result = Convert.ToInt16(Access.mySqlCommand.ExecuteNonQuery());
                    Access.ClosedAccess();
                    if (Result == 1)
                    {
                        return true;
                    }
                    return false;
                //}
                //catch (System.Exception)
                //{
                //    Access.ClosedAccess();
                //    return false;
                //}
            }
            public static DataTable GetMember()
            {
                DataTable tbl = new DataTable();
                try {
                    Access.InitAccess();
                    Access.mySqlCommand.CommandText = "Sp_Members_getall";
                    DataSet ds = new DataSet();
                    SqlDataAdapter da = new SqlDataAdapter(Access.mySqlCommand);
                    da.Fill(ds);
                    tbl = ds.Tables[0];
                    Access.ClosedAccess();
                    return tbl;
                        
                }
                catch {
                    Access.ClosedAccess();
                    return tbl;
                }
            }
            public static Int16 DeleteMembernotOrder()
            {
                try {
                    Access.InitAccess();
                    Access.mySqlCommand.CommandText = "Sp_Members_DeleteMemberNotOrder";
                    Int16 result = Convert.ToInt16(Access.mySqlCommand.ExecuteNonQuery());
                    Access.ClosedAccess();
                    if (result > 0)
                    {
                        return result;
                    }
                    else
                    {
                        return -1;
                    }
                }
                catch {
                    Access.ClosedAccess();
                    return -1;
                }
            }
            public static Int16 DeleteMember( int Member_id)
            {
                //try
                //{
                    Access.InitAccess();
                    Access.mySqlCommand.CommandText = "Sp_Members_DeleteMemberById";
                    Access.mySqlCommand.Parameters.Add("@Member_Id", SqlDbType.Int).Value = Member_id;
                    Int16 result = Convert.ToInt16(Access.mySqlCommand.ExecuteNonQuery());
                    Access.ClosedAccess();
                    if (result > 0)
                    {
                        return result;
                    }
                    else
                    {
                        return -1;
                    }
                //}
                //catch
                //{
                //    Access.ClosedAccess();
                //    return -1;
                //}
            }
            public static Int16 Kiemtraten(String  Member_Name)
            {
                //try
                //{
                Access.InitAccess();
                Access.mySqlCommand.CommandText = "Sp_Members_DeleteMemberById";
                Access.mySqlCommand.Parameters.Add("@Member_Id", SqlDbType.Int).Value = Member_Name;
                Int16 result = Convert.ToInt16(Access.mySqlCommand.ExecuteNonQuery());
                Access.ClosedAccess();
                if (result > 0)
                {
                    return result;
                }
                else
                {
                    return -1;
                }
                //}
                //catch
                //{
                //    Access.ClosedAccess();
                //    return -1;
                //}
            }
            #endregion

        }
}
